﻿using Module.FlowChart.Model;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Module.FlowChart.View
{
    internal class ViewManagerHandles
    {
        public Action<ToolStripMenuItem> AddMenu;
        public Action<string> ChangePage;
        public Func<string, Control> GetControl;
        public Func<Control> GetRightWindow;
        public Action Drawed;
        public bool IsResizing;
        public Func<Document> GetDocument;
        public Action<Control, bool> SetRightForm;
    }
    internal abstract class ViewManager
    {
        protected Document Document;
        protected Control Container;
        protected WorkPage Page;
        protected PageDrawer Drawer;
        protected ViewManagerHandles Handles;
        public bool Active { get; protected set; }

        public abstract string TypeName { get; }

        public ViewManager(ViewManagerHandles h, Document doc, WorkPage page, Control container)
        {
            Handles = h;
            Document = doc;
            Page = page;
            Container = container;
            CreatMenu();
            InitMenuState(false);

        }

        protected abstract void CreatMenu();
        protected abstract void InitMenuState(bool visible);


        public abstract void Resized();
        public virtual void Every33ms() { }
        public virtual void SetActive(bool v)
        {
            Active = v;
            InitMenuState(v);

        }
        public virtual void DestoryMgr()
        {

        }
        public virtual void OnPageNameChanged(string oldname, string newname) { }
        #region 坐标
        private float m_Scale;
        private Point m_LeftTop;
        protected virtual float Scale { get => m_Scale; set => m_Scale = value; }
        protected virtual Point LeftTop { get => m_LeftTop; set => m_LeftTop = value; }
        public Rectangle DocPosToContainerPos(Rectangle rect)
        {
            return new Rectangle(
                   (int)((rect.X - LeftTop.X) * Scale), (int)((rect.Y - LeftTop.Y) * Scale), (int)(rect.Width * Scale), (int)(rect.Height * Scale)
                );
        }
        public Point DocPosToContainerPos(int x, int y)
        {
            return new Point(
                   (int)((x - LeftTop.X) * Scale), (int)((y - LeftTop.Y) * Scale)
                );
        }
        public Point DocPosToContainerPos(Point p)
        {
            return new Point(
                   (int)((p.X - LeftTop.X) * Scale), (int)((p.Y - LeftTop.Y) * Scale)
                );
        }
        public Rectangle DocPosToContainerPos(Point point, Size size)
        {
            return new Rectangle(
                   (int)((point.X - LeftTop.X) * Scale), (int)((point.Y - LeftTop.Y) * Scale), (int)(size.Width * Scale), (int)(size.Height * Scale)
                );
        }



        public Rectangle ContainerPosToDocPos(Rectangle rect)
        {
            return new Rectangle((int)(rect.X / Scale) + LeftTop.X, (int)(rect.Y / Scale) + LeftTop.Y,
                (int)(rect.Width / Scale), (int)(rect.Height / Scale));
        }
        public Point ContainerPosToDocPos(int x, int y)
        {
            return new Point((int)(x / Scale) + LeftTop.X, (int)(y / Scale) + LeftTop.Y);
        }
        public Rectangle ContainerPosToDocPos(Point point, Size size)
        {
            return new Rectangle((int)(point.X / Scale) + LeftTop.X, (int)(point.Y / Scale) + LeftTop.Y, (int)(size.Width / Scale), (int)(size.Height / Scale));
        }
        #endregion
    }

}